﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using Klear.Library.Models;
using Klear.Library.Services;

namespace Klear.Library.ViewModels;

public class GlobalSearchViewModel : ViewModelBase
{
    private readonly IGlobalSearchService _globalSearchService;

    public ICommand LoadRecordsCommand { get; }
    public ICommand LoadCategoriesCommand { get; }
    public ICommand QueryCommand { get; }
    public ICommand ResetCommand { get; }

    //构造函数
    public GlobalSearchViewModel(IGlobalSearchService globalSearchService) {
        _globalSearchService = globalSearchService;
        LoadRecordsCommand = new AsyncRelayCommand(LoadIncomeExpensesAsync);
        LoadCategoriesCommand = new AsyncRelayCommand(LoadCategoriesAsync);
        QueryCommand = new AsyncRelayCommand(LoadSearchResults);
        ResetCommand = new AsyncRelayCommand(ResetSearchResults);
        // 初始化选项
        TitleTypes = new ObservableCollection<string> { "收入", "支出" };
        CategoryList = new ObservableCollection<string>();
        
    }
    
    //将前端获取到的值储存为属性
    //收支类别
    private string _selectedTitleType;
    public ObservableCollection<string> TitleTypes { get; } //下拉选项
    public string SelectedTitleType {
        get => _selectedTitleType;
        set {
            if (_selectedTitleType != value) {
                _selectedTitleType = value;
                OnPropertyChanged(nameof(SelectedTitleType));
            }
        }
    } // 当前选中的选项

    //金额
    private string _amount;
    private decimal _amountDecimal;
    public string Amount {
        get => _amount;
        set
        {
            if (SetProperty(ref _amount, value))
            {
                // 尝试将字符串转换为 decimal
                if (decimal.TryParse(value, out decimal result))
                {
                    _amountDecimal = result;  // 成功解析，保存为 decimal
                }
                else
                {
                    _amountDecimal = 0;  // 解析失败时，可以设置为默认值
                }
            }
        }
    }

    //分类
    private string _selectedCategory;
    public string SelectedCategory {
        get => _selectedCategory;
        set {
            if (_selectedCategory != value) {
                _selectedCategory = value;
                OnPropertyChanged(nameof(SelectedCategory));
            }
        }
    }

    //备注
    private string _notes;
    public string Notes {
        get => _notes;
        set => SetProperty(ref _notes, value);
    }

    //创建时间
    private string _createTime;
    private DateTime _createTimeDateTime;
    public string CreateTime {
        get => _createTime;
        set
        {
            if (SetProperty(ref _createTime, value))
            {
                // 尝试将字符串转换为 DateTime
                if (DateTime.TryParse(value, out DateTime result))
                {
                    _createTimeDateTime = result;  // 成功解析，保存为 DateTime
                }
                else
                {
                    _createTimeDateTime = DateTime.MinValue;  // 解析失败时，可以设置为默认值
                }
            }
        }
    }
    

    //加载本月收支数据
    private ObservableCollection<IncomeAndExpensesModel> _incomeExpensesListThisMonth = new();

    public ObservableCollection<IncomeAndExpensesModel> IncomeExpensesListThisMonth {
        get => _incomeExpensesListThisMonth;
        set => SetProperty(ref _incomeExpensesListThisMonth, value);
    }

    public async Task LoadIncomeExpensesAsync() {
        IncomeExpensesListThisMonth.Clear();

        var res = await _globalSearchService.LoadIncomeAndExpensesAsync();
        foreach (var item in res) {
            IncomeExpensesListThisMonth.Add(item);
        }
    }
    
    //加载下拉选择器：类别
    public ObservableCollection<string> CategoryList { get; }

    public async Task LoadCategoriesAsync() {
        CategoryList.Clear();

        var res = await _globalSearchService.LoadCategoriesAsync();
        foreach (var item in res) {
            CategoryList.Add(item.CategoryName);
        }
    }
    
    //加载搜索结果
    public async Task LoadSearchResults() {
        IncomeExpensesListThisMonth.Clear();
        var res = await _globalSearchService.LoadSearchResultsAsync(SelectedTitleType, _amountDecimal, SelectedCategory, Notes, _createTimeDateTime);
        foreach (var item in res) {
            IncomeExpensesListThisMonth.Add(item);
        }
    }
    
    //重置搜索结果
    public async Task ResetSearchResults() {
        Amount = string.Empty;
        Notes = string.Empty;
        CreateTime = string.Empty;
        SelectedTitleType = null;
        SelectedCategory = null;
        IncomeExpensesListThisMonth.Clear();

        var res = await _globalSearchService.LoadIncomeAndExpensesAsync();
        foreach (var item in res) {
            IncomeExpensesListThisMonth.Add(item);
        }
    }
}